package com.checkoutcheckins.client.panels;

import com.checkoutcheckins.client.clickhandlers.CloseHandler;
import com.checkoutcheckins.client.clickhandlers.ShoutButtonHandler;
import com.checkoutcheckins.client.data.foursquare.FoursquareUser;
import com.checkoutcheckins.client.rpc.FoursquareRetrival;
import com.checkoutcheckins.client.services.FoursquareServiceAsync;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;

public class SpreadWordPopupPanel extends PopupPanel {

	public SpreadWordPopupPanel() {
		super();
		init();
	}

	private void init() {
		this.setAnimationEnabled(true);
		this.setGlassEnabled(true);
		this.setPopupPosition(Window.getClientWidth() / 2 - 250, 200);
		this.setWidth("500px");
		this.setStyleName("spreadWordPopup");
	}

	public void show(final FoursquareUser fsUser) {

		CheckBox facebookCheckbox = null;
		CheckBox twitterCheckbox = null;

		this.show();

		final FlexTable ft = new FlexTable();
		ft.getFlexCellFormatter().setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		ft.setHTML(0, 0, "Sorry to interrupt... but if you're enjoying"
				+ " CheckoutCheckins would you "
				+ " mind telling your friends about us?");

		TextArea shoutBox = new TextArea();
		shoutBox.setText("Take a look at an interactive"
				+ " map of you and your friends' foursquare checkins"
				+ " at www.CheckoutCheckins.com! I did!");

		shoutBox.setWidth("490px");

		ft.setWidget(1, 0, shoutBox);
		ft.setHTML(2, 0, "<center>Send to:</center>");

		HorizontalPanel shareOptions = new HorizontalPanel();

		CheckBox foursquareCheckbox = new CheckBox(
				"<img src='images/foursquare_logo.png'>", true);
		foursquareCheckbox.setValue(true);
		foursquareCheckbox.setEnabled(false);
		shareOptions.add(foursquareCheckbox);

		if (fsUser.getTwitter() != null && !fsUser.getTwitter().equals("")) {
			twitterCheckbox = new CheckBox(
					"<img src='images/twitter_logo.png'>", true);
			shareOptions.add(twitterCheckbox);
		}

		if (fsUser.getFacebook() != null && !fsUser.getFacebook().equals("")) {
			facebookCheckbox = new CheckBox(
					"<img src='images/facebook_logo.jpg'>", true);
			shareOptions.add(facebookCheckbox);
		}

		ft.setWidget(3, 0, shareOptions);
		ft.getFlexCellFormatter().setHorizontalAlignment(3, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		ft.setHTML(4, 0,
				"<i>P.S. Don't worry, this is the only time we'll ask</i>");

		HorizontalPanel shareButtons = new HorizontalPanel();

		Button loveButton = new Button("Share the LOVE");
		loveButton.addClickHandler(new ShoutButtonHandler(this, fsUser.getId(),
				facebookCheckbox, twitterCheckbox, shoutBox));

		shareButtons.add(loveButton);

		Button noThanksButton = new Button("No Thanks");
		noThanksButton.addClickHandler(new CloseHandler(this));
		noThanksButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				FoursquareServiceAsync fsSvc = FoursquareRetrival
				.getFoursquareService();
				
				AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {

					}

					@Override
					public void onSuccess(Boolean result) {						
					}
				};
				
				fsSvc.isWordSpreadAsked(fsUser.getId(), true, callback);
			}
		});

		shareButtons.add(noThanksButton);

		ft.setWidget(5, 0, shareButtons);
		ft.getFlexCellFormatter().setHorizontalAlignment(5, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		this.add(ft);
	}
}
